package com.wu.family.utils.audio;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Environment;

public  class SoundPlayer {
	static final private double EMA_FILTER = 0.6;
	private static SoundPlayer soundPlayer = null;
	
	private MediaPlayer mMediaPlayer = new MediaPlayer();

	
	private SoundPlayer() {
	}

	public static SoundPlayer getInstance(){
		if (soundPlayer == null) {
			soundPlayer = new SoundPlayer();
		}
		return soundPlayer;
	}

	/**
	 * @Description
	 * @param path
	 */
	public void playMusic(String path) {
		try {
			if (mMediaPlayer == null) {
				mMediaPlayer = new MediaPlayer();
			}
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
			}
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(path);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public void playMusic(String path, final OnCompleteItf onCompleteItf) {
		try {
			if (mMediaPlayer == null) {
				mMediaPlayer = new MediaPlayer();
			}
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
				if (mOnCompleteItf != null) {
					mOnCompleteItf.OnUnComplete();
				}
			}
			mOnCompleteItf = onCompleteItf;
			
			mMediaPlayer.reset();
			mMediaPlayer.setVolume(1.0f, 1.0f);
			mMediaPlayer.setDataSource(path);
			mMediaPlayer.prepare();
			if (mOnCompleteItf != null) {
				mOnCompleteItf.OnPreStart(mMediaPlayer.getDuration());
			}
			mMediaPlayer.start();
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {
					if (mOnCompleteItf != null) {
						mOnCompleteItf.OnComplete();
					}
					if (onGlobalStartStopItf!=null) {
						onGlobalStartStopItf.onStop();
					}
				}
			});
			
			if (onGlobalStartStopItf!=null) {
				onGlobalStartStopItf.onStart();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public int getProgress() {
		if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
			return mMediaPlayer.getCurrentPosition()*100/mMediaPlayer.getDuration();
		}
		return 0;
	}
	
	public int getDuration() {
		if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
			return mMediaPlayer.getDuration();
		}
		return -1;
	}

	public void stop() {
		if (mMediaPlayer != null) {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
				if (onGlobalStartStopItf!=null) {
					onGlobalStartStopItf.onStop();
				}
				if (mOnCompleteItf != null) {
					mOnCompleteItf.OnUnComplete();
				}
			}
			mMediaPlayer = null;
		}
	}

	public void pause() {
		if (mMediaPlayer != null) {
			mMediaPlayer.pause();
			if (onGlobalStartStopItf!=null) {
				onGlobalStartStopItf.onPause();
			}
		}
	}

	public void start() {
		if (mMediaPlayer != null) {
			mMediaPlayer.start();
			if (onGlobalStartStopItf!=null) {
				onGlobalStartStopItf.onStart();
			}
		}
	}

	OnCompleteItf mOnCompleteItf;
	public interface OnCompleteItf{
		public void OnPreStart(int duration);
		public void OnComplete();
		public void OnUnComplete();
	}
	
	OnGlobalStartStopItf onGlobalStartStopItf;
	public OnGlobalStartStopItf getOnGlobalStartStopItf() {
		return onGlobalStartStopItf;
	}

	public void setOnGlobalStartStopItf(OnGlobalStartStopItf onGlobalStartStopItf) {
		this.onGlobalStartStopItf = onGlobalStartStopItf;
	}
	public interface OnGlobalStartStopItf{
		public void onStart();
		public void onStop();
		public void onPause();
	}
}
